home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Environments / Chipmunk Basic 3.3.9 / chipmunk-basic quick-ref < prev    next >
Encoding:
Text File  |  1996-06-08  |  17.9 KB  |  428 lines  |  [TEXT/ttxt]

  1. /*
  2.  * Quick Reference and FAQ for Chipmunk Basic - MacOS version
  3.  * chipmunk-basic-3.3.9.sit.hqx - 96Jun 
  4.  *
  5.  * Chipmunk Basic is (c) Copyright 1994,1996 Ronald H. Nicholson, Jr.
  6.  * ALL RIGHTS RESERVED
  7.  *
  8.  * This program is distributed in the hope that it will be useful, but
  9.  * WITHOUT ANY WARRANTY OF ANY KIND; not even the implied warranty of
  10.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11.  
  12. Updates to this document may be found on the WWWeb at:
  13.   http://www.nicholson.com/rhn/basic/  
  14.  
  15. ---
  16. Quick Reference for Chipmunk Basic
  17. ported to the MacOS by Ronald H. Nicholson, Jr.      rhn@nicholson.com 
  18.  
  19. Supported operators, functions and statements (reserved words):
  20.  
  21.     + - * / ^ mod  and or xor not  > < >= <= <> =
  22.     sqr() log() exp() sin() cos() tan() atn()  pi
  23.     abs() sgn() int() rnd() peek() val() asc() len()
  24.     mid$() right$() left$() str$() chr$()  lcase$() ucase$()
  25.     goto  if then else endif  gosub return
  26.     for to step next  while wend  select case
  27.     rem  let  dim erase  data read restore   field$()
  28.     input print open for output append as close# load save
  29.     random lof loc get put   
  30.     inkey$  input$ eof()  files  fgetbyte# fseek# fputbyte
  31.     run stop end exit quit cont  renum  new clear
  32.     date$ time$ timer  sound morse say  doevents
  33.     home cls gotoxy htab vtab pos() 
  34.     graphics sprite pset moveto lineto window scrn mouse()
  35.     varptr peek() poke fre push() pop  isarray
  36.     sub call usr  def fn
  37.     type class extends  string integer single double
  38.     asin acos sinh cosh tanh log10 floor true false ubound
  39.  
  40.     
  41. - The "basic.man" file is included with the distribution.  It describes
  42.   the above functions and statements in more detail.
  43. - Line numbers are required for console input.
  44. - The interpreter includes an dumb old fashioned line number based
  45.   editor.  (enter a line, delete a line, edit one line at a time).
  46. - The variable types are long floats, short integers and strings with
  47.   a maximum length of 254 characters.  Variable names can be up to
  48.   31 characters long and are case insensitive.
  49. - Math is done using the built-in double precision IEEE math.
  50.   All trigonometric functions express angles in radians.
  51.  
  52. ---
  53. Some Simple Examples:
  54.  
  55.   for i=1 to 10 : print i : next i
  56.  
  57.   c$ = a$ + b$            : rem string concatenation
  58.   a$ = inkey$            : rem inkey$ is non-blocking 
  59.   
  60.   load "sieve.bas"        : rem load TEXT file as a Basic program
  61.                   : rem file names should have .bas extension
  62.   
  63.   open "filename" for output as #1 : print #1, "hello world" : close #1 
  64.   
  65.   gotoxy 5,10 : print "here"    : rem text window (0,0 origin)
  66.   x = pos(0)            : rem text cursor horizontal location
  67.   y = pos(-1)            : rem vertical text cursor location
  68.   
  69.   graphics 0  : moveto 10,10 : lineto 110,80    : rem draw graphics line
  70.   
  71. ---
  72. Frequently Asked Questions:
  73.  
  74. Q: Is there more documentation on learning how to program in the
  75.    BASIC language?
  76. A: The language is mostly compatible with books on programming in the
  77.    BASIC language that were published between 1978 and 1988.  Since these
  78.    are mostly out of print, your best bet is to try your local public
  79.    library.
  80.    
  81.    Also, there is a short tutorial on the Chipmunk Basic web page:
  82.         http://www.nicholson.com/rhn/basic/
  83.  
  84. Q: Is there any way to redirect print statements to a line printer?
  85. A: No, there is no way to make this work with current MacOS printer
  86.    drivers. I recommend printing to a file, and then using SimpleText
  87.    to print the file.
  88.  
  89.    Depending on what kind of printer you have, the command
  90.        call "printText"
  91.    might send the last 66 lines of text from the console window to your
  92.    printer (US letter size only.)
  93.  
  94. Q: Why can't I load a text file using the "Open" menu?
  95. A: Chipmunk Basic requires that Basic program file names end with a
  96.    ".bas" suffix. However, if you hold down the option key, Chipmunk
  97.    Basic will try to open any TEXT file.
  98.  
  99. Q: How do I talk to the serial ports using Chipmunk Basic?
  100. A: Chipmunk Basic requires that the Communications ToolBox "Serial Tool"
  101.    be installed in the Extensions folder in your System Folder.  First
  102.    open "COM1:" for input, followed by opening "COM1:" for output.
  103.    The errorstatus$ string variable will return a string you can use
  104.    instead of "COM1:" when you don't want the CTB setup dialog box.
  105.    Try the following snippet:
  106.    
  107.     100 rem   ***  Using the CTB serial ports  ***
  108.     110 on error goto 200
  109.     120 open "COM1:" for input as #3 : altsetup$ = "COM1: "+errorstatus$
  110.     130 open "COM1:" for output as #4
  111.     150 while not mouse(0)
  112.     160   if not eof(3) then i = fgetbyte(3) : print chr$(i);
  113.     170   c$ = inkey$ : if len(c$) > 0 then print #4,c$;
  114.     180 wend
  115.     190 print " closing serial ports" : close #4 : close #3
  116.     200 print errorstatus$ : goto 190
  117.  
  118.    The command
  119.         CALL "sendbreak", n
  120.    will send a break for n ticks (60ths of a second).
  121.    
  122.    If you can't use the Communications ToolBox Serial Tool for any
  123.    reason,you can try using
  124.         open "COM3: 19200" for input as #3    ' note the COM3:
  125.    instead.  This uses the serial port driver directly (serial port
  126.    only), and isn't as compatible as using the Communications ToolBox.
  127.  
  128. Q: How do I use sprites?
  129. A: Sprite are little icons that can be moved around in the graphics
  130.    window.  You can use up to 15 sprites, numbered from 1 to 15  Sprites
  131.    will appear in front of any graphics or picts in the graphics window.
  132.    The sprites are in layers; sprite 1 will pass in front of sprite 2
  133.    when they overlap, etc. 
  134.    
  135.    You specify what you want a particular sprite to look like by using
  136.    a resource ID.  For instance a sprite with a resource id of 28 looks
  137.    like the Chipmunk icon, with a resource id of 130 looks like a ball.
  138.    One with a rsrc id of 0 is invisible.
  139.    
  140.    You may place sprites at an x,y coordinate.  Or you may move them
  141.    around and leave a trail behind them to draw things.  Sprite movement
  142.    commands include { up, down, left, right, forward, turn, turnleft,
  143.    turnright, turnto, penup and pendown }.  To get rid of a sprite,
  144.    simply move them offscreen (e.g. { x,y } = { -1,-1} ).
  145.  
  146. Q: How do I make my own sprites?
  147. A: I use ResEdit (available from ftp.apple.com).  Just paste the new
  148.    sprite 'ICN#' and 'cicn's into the resource fork of a file.  Then
  149.         open "myresfile.rsrc" for data input
  150.    will use the named resource file for sprite (pict & sound) resources.
  151.    Resource ID's 128-160 are reserved for the Chipmunk Basic interpreter,
  152.    so please use a high resource ID number.  A 'cicn' resource requires
  153.    a 'ICN#' resource with the same ID number.  You can use the same
  154.    method for creating custom PICT and sound resources.  You can also
  155.    paste resources directly into your .bas program text file.
  156.    
  157. Q: Why can't I make my graphics window bigger?
  158.    If you have enough memory in your Mac, try increasing the Preferred
  159.    Memory requirements of Chipmunk Basic using the Finder Get Info dialog.
  160.    Also try decreasing the number of colors in the Monitors Control Panel.
  161.  
  162. Q: Why won't this Microsoft QuickBasic program run? ...
  163. A: Chipmunk Basic was never intended to be exactly compatible with any
  164.    other commercial BAISC language product.  It does include most of the
  165.    features of ANSI BASIC language definition.  Many BASIC programs that
  166.    are saved as ASCII text files and that don't use special features of
  167.    the computer system can be easily ported to Chipmunk Basic.
  168.    
  169. Q: Can you send me a list of different peeks and pokes?
  170. A: Peeks and pokes don't have much use on current Mac Systems.  If you
  171.    want to live dangerously, you might want to get a copy of SysEqu.h or
  172.    LoMem.h from an old C or 68K assembly language programming package;
  173.    but don't expect anything you do to be compatible with the latest
  174.    MacOS System software.
  175.  
  176. Q: Is there a compiler for Chipmunk Basic?
  177. A: No, however there are several commercial Basic compilers on the market
  178.    (Staz FutureBasic, True Basic, VIP Basic, Oracle Power Objects ...)
  179.    and rumors of major new support for the BASIC language on the Mac
  180.    coming next year (1997 maybe ?,  Kenobi ??, Denali ??? ... ).
  181.    
  182. Q: What about Object Oriented Programming with Chipmunk Basic?
  183. A: OOP extensions to Chipmunk Basic (classes, objects, inheritance,
  184.    methods, etc.) are currently in beta test.  Documentation is available
  185.    from the Chipmunk Basic web page: http://www.nicholson.com/rhn/basic/
  186.  
  187. Q: What is the history of Chipmunk Basic?
  188. A: In March 1990, p2c, a pascal to c translator, was posted to the usenet
  189.    newsgroup "comp.source.misc" by David Gillespie.  One of the test
  190.    input files was "basic.p".  I used the output c file to learn about
  191.    language interpreters and then ported the resulting Basic interpreter
  192.    to a Mac 512KE and then to a PowerMac 7100.  Chipmunk Basic for
  193.    the PowerMac was posted to Info-Mac on March 15th, 1994, the second
  194.    day that PowerMacintosh's were available to the public.
  195.  
  196. ---
  197. Important Changes:
  198.   3.2.9:  DoScript AppleEvents can return strings as well as numbers
  199.   3.3.2:  fixed sprite turn directions
  200.   3.3.5:  fixed integer rounding direction
  201.   3.3.7:  type & class vars now require "dim ... as new ..." syntax
  202.           assignment to variable "this" in object methods fixed
  203.  
  204. ---
  205. Additional Chipmunk Basic Macintosh specific commands and functions:
  206.  
  207. - Mac Graphics
  208.   * note: all graphics commands in version 3.x are experimental & beta test *
  209.     
  210.     GRAPHICS 0                '// show graphics window
  211.     graphics -1                '// hide graphics window
  212.     graphics WINDOW x, y, width, height    '// graphics window setup
  213.  
  214.     graphics MOVETO  x,y        '// Move pen To x,y
  215.     graphics LINETO  x,y        '// draw Line To x,y
  216.     graphics CIRCLE  x        '// circle of diameter x
  217.     graphics OVAL    x,y        '// oval  x wide by y high
  218.     graphics DRAWTEXT  a$        '// DrawText
  219.         '// use MOVETO x,y to position text & center circles
  220.     pset x,y            '// plot 1 point at x,y
  221.     
  222.     graphics RECT  x1,y1, x2,y2        '// FrameRect
  223.     graphics FILLRECT  x1,y1, x2,y2,pat#    '// PaintRect
  224.     graphics FILLRECT  x1,y1, x2,y2,-1    '// EraseRect
  225.     graphics OVAL  x1,y1, x2,y2        '// FrameOval
  226.     graphics FILLOVAL  x1,y1, x2,y2,pat#    '// PaintOval
  227.     graphics PENSETUP  xsize, ysize [, mode, pat# ]
  228.     
  229.     graphics COLOR  r,g,b        '// red green blue 0-100%
  230.     graphics COLOR  0,0,0        '// use default black
  231.     graphics COLOR  i        '// set RGBForeColor to index i
  232.     graphics PICT   x,y,n        '// draw 'pict' resource n
  233.     graphics TEXTSETUP f, s, m    '// set up text font, size, mode
  234.  
  235.     graphics (-38)            '// get graphics window max width
  236.     graphics (-39)            '// get graphics window max height
  237.     graphics (-34)            '// get graphics window pixel depth
  238.  
  239.     SPRITE    n  x, y, id        '// sprite n @ x,y using ICN# id
  240.                     '// n from 1 to 15, 1 frontmost
  241.                     '// ICN# 128 - 141 included with app
  242.                     '// cicn requires ICN# with same id.
  243.     sprite    n  UP    x        '// sprite #n move up x pixels
  244.     sprite  n  DOWN  x        '//   also returns new sprite y coord
  245.     sprite  n  LEFT  x        '// move LEFT (not turn as in Logo!)
  246.     sprite  n  RIGHT   x
  247.     sprite    n  TURN    d        '// turn counter-clockwise d degrees
  248.     sprite    n  TURNRIGHT d        '// turn clockwise d degrees
  249.     sprite    n  TURNLEFT  d
  250.     sprite    n  TURNTO    d        '// turn to d degrees from x axis
  251.     sprite    n  FORWARD x        '// move forward x pixels
  252.     sprite    n  PENUP
  253.     sprite    n  PENDOWN
  254.     sprite    n  COLLISION        '// returns 1st sprite# in contact
  255.     sprite  n  TO  x,y        '// paste a fixed sprite image @(x,y)
  256.     
  257.     graphics WINDOW -1,-1, x,y, 2    '// scroll graphics background
  258.  
  259.     ' Too small a "Preferred Size" (as set from Finder Get Info window)
  260.     ' may limit the maximum graphics window size.
  261.     
  262.     mouse(0)    returns 1 if the mouse button is down
  263.     mouse(1)    x coordinate in current window
  264.     mouse(2)    y coordinate in current window
  265.     mouse(3)    last x click location (set to -1 after read)
  266.     mouse(4)    last y click location (set to -1 after read)
  267.  
  268. - Mac Sound commands
  269.  
  270.       morse "CQ DE N6YWU",16,40,13,700    '// play morse code
  271.         '// MORSE string, dot_wpm, volume, letter_wpm, freq
  272.   
  273.     say "hello world"        '//  *requires* the Speech Manager
  274.     say "faster", 196, 44, 1    '//  string, rate, pitch, voice#
  275.     say        '// reads the last 12 lines of the console window
  276.     x  = macfunction("numSpeakers")    '// the number of voices
  277.     v$ = macfunction("getSpeaker")    '// get the voices name
  278.     
  279.     sound 440, 0.5, 30    '// freq, secs_duration, vol(0-100)
  280.     sound 0, 8192        '// play snd resource #8192
  281.     sound -1, a(0),11100    '// play 1 sec. of samples from array a()
  282.     sound -2, midi_voice, midi_key, velocity, secs_dur [,channel]
  283.         '// play Quicktime midi synth, requires Quicktime 2.0
  284.     call "record", a(0), n  '// records n samples of sound into a()
  285.     
  286. - Mac User interface functions and commands
  287.         
  288.     call "bigfont"        '// toggles BIGFONT console output status
  289.     call "hideMenuBar"    '// hides menubar. graphics -1 to restore.
  290.     call "wintitle", t$    '// sets the graphics window title
  291.     
  292.     graphics BUTTON title$,x,y,w,h,key_code
  293.             '// draw a standard button at (x,y) size (w,h)
  294.             '// will input chr$(key_code) when pressed
  295.     graphics BUTTON title$,x,y,w,h,e$
  296.             '// button will exec(e$) when pressed
  297.     graphics BUTTON "",x,y,w,h,e$    '// transparent button
  298.     graphics BUTTON "",x,y,id,e$,-4    '// cicn or ICN# id button
  299.     graphics BUTTON "",-1        '// remove all buttons & sliders
  300.     graphics EDIT a$ [,w,h]        '// edit a$ in a text edit box
  301.  
  302.         a$ = inputbox("prompt", "title", "default", id)
  303.                 '// if id > 1024 uses custom DLOG #id
  304.                 '//   item 1 = OK, item 2 = cancel
  305.                 '//   prompt replaces 1st static text item
  306.                 '//   default replaces 1st edit text item
  307.                 
  308.         call "menu", 4, title$, itemlist$, eventlist$
  309.         call "menu", 4, "myMenu", "item1;item2", "foo1();foo2()"
  310.             '// sets up the 4th menu (only 4th menu works now)
  311.             '// call sub's foo1() and foo2() on menu select
  312.         doevents()    '// poll for button and menu events
  313.     
  314. - Additional Mac specific functions and commands:
  315.  
  316.     open "SFGetFile" for input as #2    '// "SFPutFile" for output
  317.     open "COM1:" for input as #1        '//  uses CTB Serial Tool
  318.     open "COM3: 9600" for input as #3   '//  uses serial driver
  319.     open f$ for data input as #4    '// data file, no cr-lf translation
  320.     open f$ for data input        '// use file for resources, picts, etc.
  321.     a$ = field$("aa bb cc", 2," ")    '// returns "bb", (awk fields)
  322.     
  323.     call "wait", n            '// waits for n seconds
  324.     call "printText"  '// sends last ~66 console lines to printer
  325.     
  326.     call "putPref","512",7    '// increase scrollback buffer to 512
  327.         '// you will need to increase the preferred memory size
  328.         '// by 1K for every +10 lines above 256
  329.     call "sendbreak",t  '// serial port break for t ticks (open 1st)
  330.     call varptr(a%(0))    '// calls 68k code located in array a%
  331.                 '// 68k code works even on PPC
  332.     call -151    '// drops into MacsBug (must be installed !!!)
  333.     call "beep", i  '// calls BCMD resource "beep" with 1 long* param
  334.         reply$ = macfunction("Gestalt","cput")  '// system gestalt info
  335.         x = macfunction("keydown", 0x3a)     '// test option key
  336.     call "launch", appname$     '// launch an app in local dir
  337.         
  338. - Sending AppleScript and AppleEvents
  339.     
  340.     call "doscript", appname$, script$    '// send a DoScript AE
  341.     macfunction("doscript", app$, script$)    '// also returns result
  342.     call "doscript",sig$,url$,class$, id$    '// sends w/ directObject
  343.     call "doscript","MOSS", url$, "GURL", "GURL"
  344.             '// sends a get URL event to Netscape(tm)
  345.             
  346.     call "applescript", script$  '// compiles and executes a script.
  347.             '// script$ can be a single string or a lengthy
  348.             '// (blank terminated) string array.
  349.             '// ** requires AppleScript extension **
  350.  
  351. - A few oddball commands included in version 3.3.x just for fun:
  352.                 
  353.     call "fft_polar", real(0), amp(0), phase(0) , size '// fft
  354.     call "fft_inv",   amp(0), phase(0), real(0) , size '// inverse
  355.                 '// the fft size must a power of 2 : 2^n
  356.     call "math$", "mul$", r$, "1234567890", "9876543210"
  357.         '// 200+ digit string math (add$, sub$, mul$, div$, mod$)
  358.                 
  359. - AppleScript scripting.  Try this script from a script editor:
  360.         tell application "chipbasic" to DoScript "val( 5 * 7 )"
  361.         '// The "val" function will return a result to the script editor.
  362.         tell application "chipbasic" to DoScript "eval date$"
  363.         '// exec will return a string result
  364.         tell application "chipbasic" to DoScript "say 'hello'"
  365.       
  366. - A few experimental commands that only work in the 68K Mac version:
  367.     ipaddr = macfunction("dnr2num", domain_name$)
  368.     call "fetchURL", ipaddr, httpcmd$, filename$
  369.         '// MacTCP HTTP file transfer, saves to local file
  370.         '// example command:
  371.         '// httpcmd$ = "GET /index.html HTTP/1.0"+chr$(10)+chr$(10)
  372.     '// Strip out the CFRG resource from if you want to make a 
  373.     '//  fat version of cbas into a 68K (unaccelerated) version.
  374.     
  375. - ICN#, cicn, PICT & BCMD resources may be stored the resource fork of
  376.     any file, not just the "mumble.bas" program file.  Use:
  377.     open "foo.rsrc" for data input : rem with no "as" token,
  378.         '// this makes the file "foo.rsrc" the current resource file
  379.         
  380. - bigfont options are stored in the Prefs file (edit w/ BBEdit or equiv.)
  381.  
  382. ---
  383. Other notes or Bugs:
  384.  
  385. - All input works like the LINE INPUT statement of other Basic versions.
  386. - Line numbers above 999999999 will not list.
  387. - Programs without line numbers can be loaded from a file; however
  388.     sequential line numbers will be added.  The target of a GOTO can be
  389.     a label followed by a colon. e.g.
  390.         foo: x=x-1 : if x>0 then goto foo:
  391.         
  392. - Other reserved words (don't use these!) :
  393.     msgbox  do loop until break  function
  394.     method private public local   menu dialog memstat()
  395.     draw play  bload bsave min max mat
  396.     each redim resume  static
  397.     eqv imp key is each option set width swap
  398.  
  399. ---
  400. Thanks to:
  401.  - Dave Gillespie for writing the original Pascal version of this Basic.
  402.  - Dave Betz for ideas from the original XLisp 1.6 Mac port.
  403.  - many folks from Apple Dev support, Metrowerks, c.s.m.p, etc.
  404.  - John Norstad for making available the Newswatcher TCP source code.
  405.  - Jim Stout for making available "Jim's CDEFs" with source code.
  406.  
  407. ---
  408. A  sample program:
  409.  
  410. --- cut here ---
  411. 1 rem "sieve.bas" , a prime number sieve benchmark
  412. 2 t = timer
  413. 3 dim f(8194)
  414. 4 for i = 0 to 8191 : f(i) = 1 : next i
  415. 5 s = 8191
  416. 6 for i = 0 to s
  417. 7   if f(i) = 0 then goto 11
  418. 8   p = i+i+3
  419. 9   for k = i+p to s step p : f(k) = 0 : next k
  420. 10   c = c+1
  421. 11 next i
  422. 12 print c;" primes found in ";
  423. 13 t = timer-t
  424. 14 print t;" seconds"
  425. 15 end
  426. --- cut here ---
  427. */
  428.